<?php
    $x = new Zend_Auth_Storage_Session('User_Auth');
    $y = $x->read();
    if (!empty($y))
    {
        ?>
        <style>
            #header .layout{
                padding-top: 0 !important;
            }

            .categories{
                margin-top: -6px;
            }
        </style>
        <?php
    }
?>

<div id="body" class="layout">
    <div class="wrap">
        <div class="left">
            <?php
                $request = Zend_Controller_Front::getInstance();
                $controllerName = $request->getRequest()->getControllerName();
                $actionName = $request->getRequest()->getActionName();

                $url = $controllerName . "/" . $actionName;

                $sort_tag_class = $this->sort_tag_class;
                if ($sort_tag_class == "date")
                {
                    $sort_date_class = "current selected";
                    $sort_groupsize_class = "";
                    $sort_distance_class = "";
                }
                else if ($sort_tag_class == "groupsize")
                {
                    $sort_date_class = "";
                    $sort_groupsize_class = "current selected";
                    $sort_distance_class = "";
                }
                else
                {
                    $sort_date_class = "";
                    $sort_groupsize_class = "";
                    $sort_distance_class = "current selected";
                }

                $sort_date_link = $this->baseUrl("sort/date");
                $sort_group_link = $this->baseUrl("sort/groupsize");
                $sort_distance_link = $this->baseUrl("sort/distance");

                if ($url != "index/index")
                {
                    $sort_date_link = curPageURL() . "&sort_type=date";
                    $sort_group_link = curPageURL() . "&sort_type=groupsize";
                    $sort_distance_link = curPageURL() . "&sort_type=distance";
                }
            ?>
            <ul class="sort-panel">
                <li class="s-text">Sort by:</li>
                <li>
                    <a href="<?php echo $sort_date_link; ?>" class="<?php echo $sort_date_class; ?>">
                        <span class="first">
                            <span class="last">
                                <span class="middle">
                                    <img src="<?php echo IMAGES_PATH; ?>/date-icon.png" alt="date" />Date
                                </span>
                            </span>
                        </span>
                    </a>
                </li>
                <li>
                    <a href="<?php echo $sort_group_link; ?>" class="<?php echo $sort_groupsize_class; ?>">
                        <span class="first">
                            <span class="last">
                                <span class="middle">
                                    <img src="<?php echo IMAGES_PATH; ?>/group-icon.png" alt="group" />Group size
                                </span>
                            </span>
                        </span>
                    </a>
                </li>
                <li>
                    <a href="<?php echo $sort_distance_link; ?>" class="<?php echo $sort_distance_class; ?>">
                        <span class="first">
                            <span class="last">
                                <span class="middle">
                                    <img src="<?php echo IMAGES_PATH; ?>/distance-icon.png" alt="distance" />Distance
                                </span>
                            </span>
                        </span>
                    </a>
                </li>
            </ul>
            <div class="cf"></div>
            <div class="post-list">
                <?php
                    if (!empty($this->allEvents))
                    {
                        $model = new Application_Model_Common();
                        $all_event_i = "1";
                        foreach ($this->allEvents as $a_key => $a_value)
                        {
                            $event_id = $a_value["event_id"];
                            $event_title = $a_value["event_title"];
//                            $event_time = date("H:m", strtotime($a_value["event_time"])) . " " . TIME_ZONE;
                            $event_time = date("g:i a", strtotime($a_value["event_time"]));
                            $event_type = ucwords($a_value["event_type"]);

                            if ($event_type == "Private")
                            {
                                $x = new Zend_Auth_Storage_Session('User_Auth');
                                $y = $x->read();
                                if (empty($y))
                                {
                                    $event_time = "--";
                                }
                                else
                                {
                                    $user_id = $x->read()->user_id;
                                    $is_already_joined = $model->is_exists(TABLE_EVENT_JOIN, "ej_event_id=" . $event_id . " AND ej_user_id = $user_id AND ej_is_accepted = '1'  AND ej_is_invited = '0'");
//                                    prd($is_already_joined);
                                    if (!empty($is_already_joined))
                                    {
                                        $event_time = date("g:i a", strtotime($a_value["event_time"]));
                                    }
                                    else
                                    {
                                        $event_time = "--";
                                    }
                                }
                            }



                            $event_group_size = $a_value["event_group_size"];
                            if (empty($event_group_size))
                                $event_group_size = "UL";

                            $event_date_array = json_decode($a_value["event_date"]);
//                            $event_date = $event_date_array->day[0] . " " . date("F", strtotime($event_date_array->month[0])) . " " . $event_date_array->year[0];
                            $event_date = getEventDate($a_value["event_date"]);

                            $event_address = stripslashes(trim($a_value["event_address"] . ", " . $a_value["event_location"]));
                            $event_info = stripslashes(trim($a_value["event_info"]));

                            $category_id = $a_value["category_id"];
                            $category_name = $a_value["category_name"];
                            $subcategory_id = $a_value["subcategory_id"];
                            $subcategory_name = $a_value["subcategory_name"];
                            $creator_full_name = $a_value["user_first_name"] . " " . $a_value["user_last_name"];
                            if ($creator_full_name == " ")
                                $creator_full_name = "NA";

                            $first_post_class = "";
                            if ($all_event_i == "1")
                                $first_post_class = " first";

                            $event_images = getEventImages($event_id);
                            $event_link = $this->baseUrl("eventdetail/" . getEncryptedString($event_id));
                            ?>
                            <div class="post <?php echo $first_post_class; ?>">
                                <div class="fl pimages">
                                    <div class="i-post"><img src="<?php echo $event_images[0]; ?>" alt="<?php echo $event_title; ?>" /></div>
                                    <div class="i-thumbs">

                                        <?php
                                        $event_image_i = "1";
                                        foreach ($event_images as $i_key => $i_value)
                                        {
                                            $event_image_url = $i_value;

                                            $event_active = "";
                                            if ($event_image_i == "1")
                                                $event_active = "actived";
                                            ?>
                                            <a class="<?php echo $event_active; ?>" href="<?php echo $event_image_url; ?>"><img src="<?php echo $event_image_url; ?>" alt="<?php echo $event_title; ?>" /></a>
                                            <?php
                                            $event_image_i++;
                                            if ($event_image_i == "5")
                                                break;
                                        }
                                        ?>

                                        <div class="cf"></div>
                                    </div>
                                    <!--                                    <div class="social twitter">
                                                                            <div class="count">24k</div>
                                                                        </div>
                                                                        <div class="social face">
                                                                            <div class="count">13k</div>
                                                                        </div>-->
                                </div>
                                <div class="fl content">
                                    <div class="title">
                                        <span class="event-title-in-list">
                                            <a class="p-name" href="<?php echo $event_link; ?>"><?php echo $event_title; ?></a>
                                        </span>
                                        <span class="p-cates">
                                            in <a href="<?php echo $this->baseUrl("event/search?cat_search=$category_id"); ?>"><?php echo $category_name; ?></a> / <a href="<?php echo $this->baseUrl("event/search?subcat_search=$subcategory_id"); ?>"><?php echo $subcategory_name; ?></a>
                                        </span>
                                        <div class="cf"></div>
                                    </div>
                                    <span class="p-time"><?php echo $event_date . " / " . $event_time; ?></span>
                                    <div class="infos">
                                        <span class="p-user"><?php echo $creator_full_name; ?></span><span>/</span>

                                        <?php
                                        $another_address = $this->another_address;
//                                        if (isset($_GET["location"]))
//                                        {
//                                            $another_address = urldecode($_GET["location"]);
//                                        }

                                        if ($controllerName == "event" && $actionName == "search")
                                        {
//                                               prd($event_address." --- ".$another_address);
//                                            $event_distance = getEventDistanceAway($event_address, $another_address);
                                            $event_distance = getEventDistanceAway($a_value["event_location"], $another_address);
//                                            prd($event_distance);
                                            if ($event_distance != "NA")
                                            {
                                                $event_distance = $event_distance . " Miles";
                                            }
                                            else
                                            {
                                                $event_distance = "--";
                                            }

                                            echo '<span class="miles">' . $event_distance . '</span><span>/</span>';
                                        }
                                        else if (isset($_SESSION['search']['location']))
                                        {
                                            $event_distance = getEventDistanceAway($a_value["event_location"], $_SESSION['search']['location']);
//                                            prd($event_distance);
                                            if ($event_distance != "NA")
                                            {
                                                $event_distance = $event_distance . " Miles";
                                            }
                                            else
                                            {
                                                $event_distance = "--";
                                            }

                                            echo '<span class="miles">' . $event_distance . '</span><span>/</span>';
                                        }
                                        else
                                        {
                                            echo '<span class="miles">--</span><span>/</span>';
                                        }
                                        ?>

                                                                                                                                    <!--<span class="miles"><?php echo getCurrentEventDistance($event_address); ?></span><span>/</span>-->
                                                                                                                                    <!--<span class="miles">--</span><span>/</span>-->
                                        <span class="group">Group size</span><span><?php echo $event_group_size; ?></span>
                                        <span class="publish fr"><a href="#"><?php echo $event_type; ?></a></span>
                                        <div class="cf"></div>
                                    </div>
                                    <div class="short-desc">
                                        <p><?php echo $event_info; ?></p>
                                    </div>
                                    <div>
                                        <a href="<?php echo $event_link; ?>" class="rbox yebutton fr">Event Details</a>
                                    </div>
                                </div>
                                <div class="cf"></div>
                            </div>
                            <?php
                            $all_event_i++;
                        }
                    }
                    else
                    {
                        echo '<p style="padding:10px;">No results found</p>';
                    }
                ?>
            </div>
            <?php
                echo $this->paginationControl($this->paginator, 'Sliding', 'pagination/pagination.phtml');
            ?>
        </div>
        <div class="right">
            <div class="rtitle">
                Celebrity Events
            </div>
            <div class="box celevent">
                <?php
                    if (!empty($this->celebrityEvents))
                    {
                        $celeb_i = "1";
                        foreach ($this->celebrityEvents as $c_key => $c_value)
                        {
                            $event_id = $c_value["event_id"];
                            $enc_event_id = getEncryptedString($event_id);
                            $event_title = $c_value["event_title"];
//                            $event_time = date("H:m", strtotime($c_value["event_time"])) . " " . TIME_ZONE;
                            $event_time = date("g:i a", strtotime($c_value["event_time"]));
                            $event_spots = $c_value["event_group_size"] . " spots";

                            $event_images = getEventImages($event_id);

                            $event_date_array = json_decode($c_value["event_date"]);
                            $event_date = $event_date_array->day[0] . " " . date("F", strtotime($event_date_array->month[0])) . " " . $event_date_array->year[0];
                            $event_link = $this->baseUrl("eventdetail/$enc_event_id");
                            ?>
                            <div class="boximg">
                                <img src="<?php echo $event_images[0]; ?>" alt="<?php echo $event_title; ?>" />
                                <div class="content">
                                    <p class="title"><?php echo $event_title; ?></p>
                                    <div class="link"><a href="<?php echo $event_link; ?>"><?php echo $event_link; ?></a></div>
                                    <div class="date"><?php echo $event_date; ?></div>
                                    <div class="clock"><?php echo $event_time; ?></div>
                                    <div class="info"><?php echo $event_spots; ?></div>
                                </div>
                            </div>
                            <?php
                            $celeb_i++;
                            if ($celeb_i == "3")
                            {
                                $celeb_i = "1";
                                echo '<div class="cf"></div>';
                            }
                        }
                    }
                    else
                    {
                        echo '<p style="padding:10px;">No celebrity events</p>';
                    }
                ?>
                <div class="cf"></div>
                <div class="browser-event">
                    <a class="tlink" href="<?php echo $this->baseUrl("event/celebrityevents"); ?>">Browse Celebrity Events  ></a>
                </div>
            </div>
            <div class="rtitle sponsored-title">
                Sponsored Events
            </div>
            <div class="box sponsored-events">
                <?php
                    if (!empty($this->sponsoredEvents))
                    {
                        foreach ($this->sponsoredEvents as $s_key => $s_value)
                        {
                            $event_id = $s_value["event_id"];
                            $enc_event_id = getEncryptedString($event_id);
                            $event_title = $s_value["event_title"];
//                            $event_time = date("H:m", strtotime($s_value["event_time"])) . " " . TIME_ZONE;
                            $event_time = date("g:i a", strtotime($s_value["event_time"]));

                            $event_images = getEventImages($event_id);

                            $event_date_array = json_decode($s_value["event_date"]);
                            $event_date = $event_date_array->day[0] . " " . date("F", strtotime($event_date_array->month[0])) . " " . $event_date_array->year[0];
                            $event_link = $this->baseUrl("eventdetail/$enc_event_id");
                            ?>
                            <div class="quick-post">
                                <div class="pimg fl">
                                    <img src="<?php echo $event_images[0]; ?>" alt="<?php echo $event_title; ?>" />
                                </div>
                                <div class="content">
                                    <div class="title">
                                        <span class="event-title-in-list side-list" style="display: block;">
                                            <a class="p-name" href="<?php echo $event_link; ?>"><?php echo $event_title; ?></a>
                                        </span>
                                        <span class="p-cates">
                                            <span>in</span> <a href="<?php echo $this->baseUrl("event/search?cat_search=" . $s_value["category_id"]); ?>"><?php echo $s_value["category_name"]; ?></a> / <a href="<?php echo $this->baseUrl("event/search?subcat_search=" . $s_value["subcategory_id"]); ?>"><?php echo $s_value["subcategory_name"]; ?></a>
                                        </span>
                                    </div>
                                    <div class="infos">
                                        <span class="p-time fl"><?php echo $event_date . " / " . $event_time; ?></span>
                                        <div class="cf"></div>
                                        <span class="group">Group size</span>
                                        <span><?php echo $s_value["event_group_size"]; ?></span>
                                    </div>
                                </div>
                                <div class="cf"></div>
                            </div>
                            <?php
                        }
                    }
                    else
                    {
                        echo '<p style="padding:10px;">No sponsored events</p>';
                    }
                ?>
                <!--                <div class="browser-event">
                                    <a class="tlink" href="<?php echo $this->baseUrl("event/sponsoredevents"); ?>">Browse Sponsored Events  ></a>
                                </div>-->
            </div>
            <?php
                echo $this->partial("ads.phtml", array("ads_count" => "2"));
            ?>
        </div>
        <div class="cf"></div>
    </div>
</div>